package com.example.dell.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.Target;

/**
 * Created by hzy on 2019/1/10
 * 图片加载
 *
 * @author Administrator
 */
public class ImageLoaderUtil {

    /**
     * 圆形头像
     */
    public static void LoadCircleImage(Context context, Object url, ImageView imageView) {
        Glide.with(context).load(url)
                .apply(RequestOptions.centerCropTransform()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .apply(RequestOptions.circleCropTransform())
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }

    /**
     * 常规使用
     */
    public static void LoadImage(Context context, Object url, ImageView imageView) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }


    /**
     * 自定义RequestOptions使用
     */
    public static void LoadImage(Context context, Object url, ImageView imageView,
                                 RequestOptions requestOptions) {
        Glide.with(context).load(url)
                .apply(requestOptions)
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }

    /**
     * 自定义RequestOptions使用
     */
    public static void LoadImage(Fragment fragment, Object url, ImageView imageView,
                                 RequestOptions requestOptions) {
        Glide.with(fragment).load(url)
                .apply(requestOptions)
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }


    /**
     * 需要回调时使用
     */
    public static void LoadImage(Context context, Object url, ImageViewTarget imageViewTarget) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageViewTarget);
    }

    /**
     * 需要回调时使用
     */
    public static void LoadImage(Context context, Object url, ImageView imageView,
                                 RequestListener listener) {
        Glide.with(context).load(url)
                //.thumbnail(0.1f)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .listener(listener)
                .into(imageView);
    }

    /**
     *  显示固定比例的图片16:9的图片
     * @param context
     * @param imageUrl
     * @param errorId
     * @param imageView
     */

    public static void loadIntoUseFitWidth(Context context, final String imageUrl, int errorId, final ImageView imageView) {
        Glide.with(context).load(imageUrl)
                //.thumbnail(0.1f)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {


                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        //float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = (int) ((float) vw / (float) 1.78);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .into(imageView);

        //        Glide.with(context)
        //                .load(imageUrl)
        //                .apply(new RequestOptions()
        //                        .diskCacheStrategy(DiskCacheStrategy.ALL))
        ////                .error(errorId)
        //                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        //                .listener(new RequestListener<String, GlideDrawable>() {
        //                    @Override
        //                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        //                        return false;
        //                    }
        //
        //                    @Override
        //                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        //
        //                        return false;
        //                    }
        //                })
        //                .transform(new CornersTransform(context, DensityUtils.dp2px(context,5)))
        //                .into(imageView);
    }


}
